home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / efopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  2.3 KB  |  133 lines

  1. /* efopen.c
  2.    Open a stdio file with appropriate permissions.  */
  3.  
  4. #include "uucp.h"
  5.  
  6. #include "uudefs.h"
  7. #include "sysdep.h"
  8. #include "system.h"
  9.  
  10. #include <errno.h>
  11.  
  12. #if HAVE_FCNTL_H
  13. #include <fcntl.h>
  14. #else
  15. #if HAVE_SYS_FILE_H
  16. #include <sys/file.h>
  17. #endif
  18. #endif
  19.  
  20. #ifndef O_RDONLY
  21. #define O_RDONLY 0
  22. #define O_WRONLY 1
  23. #define O_RDWR 2
  24. #endif
  25.  
  26. #ifndef O_APPEND
  27. #ifdef FAPPEND
  28. #define O_APPEND FAPPEND
  29. #endif
  30. #endif
  31.  
  32. #ifndef O_NOCTTY
  33. #define O_NOCTTY 0
  34. #endif
  35.  
  36. #ifndef FD_CLOEXEC
  37. #define FD_CLOEXEC 1
  38. #endif
  39.  
  40. FILE *
  41. esysdep_fopen (zfile, fpublic, fappend, fmkdirs)
  42.      const char *zfile;
  43.      boolean fpublic;
  44.      boolean fappend;
  45.      boolean fmkdirs;
  46. {
  47.   int imode;
  48.   int o;
  49.   FILE *e;
  50.  
  51.   if (fpublic)
  52.     imode = IPUBLIC_FILE_MODE;
  53.   else
  54.     imode = IPRIVATE_FILE_MODE;
  55.  
  56.   if (! fappend)
  57.     o = creat ((char *) zfile, imode);
  58.   else
  59.     {
  60. #ifdef O_CREAT
  61.       o = open ((char *) zfile,
  62.         O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY,
  63.         imode);
  64. #else
  65.       o = open ((char *) zfile, O_WRONLY | O_NOCTTY);
  66.       if (o < 0 && errno == ENOENT)
  67.     o = creat ((char *) zfile, imode);
  68. #endif /* ! defined (O_CREAT) */
  69.     }
  70.  
  71.   if (o < 0)
  72.     {
  73.       if (errno == ENOENT && fmkdirs)
  74.     {
  75.       if (! fsysdep_make_dirs (zfile, fpublic))
  76.         return NULL;
  77.       if (! fappend)
  78.         o = creat ((char *) zfile, imode);
  79.       else
  80.         {
  81. #ifdef O_CREAT
  82.           o = open ((char *) zfile,
  83.             O_WRONLY | O_APPEND | O_CREAT | O_NOCTTY,
  84.             imode);
  85. #else
  86.           o = creat ((char *) zfile, imode);
  87. #endif
  88.         }
  89.     }
  90.       if (o < 0)
  91.     {
  92.       ulog (LOG_ERROR, "open (%s): %s", zfile, strerror (errno));
  93.       return NULL;
  94.     }
  95.     }
  96.  
  97. #ifndef O_CREAT
  98. #ifdef O_APPEND
  99.   if (fappend)
  100.     {
  101.       if (fcntl (o, F_SETFL, O_APPEND) < 0)
  102.     {
  103.       ulog (LOG_ERROR, "fcntl (%s, O_APPEND): %s", zfile,
  104.         strerror (errno));
  105.       (void) close (o);
  106.       return NULL;
  107.     }
  108.     }
  109. #endif /* defined (O_APPEND) */
  110. #endif /* ! defined (O_CREAT) */
  111.  
  112.   if (fcntl (o, F_SETFD, fcntl (o, F_GETFD, 0) | FD_CLOEXEC) < 0)
  113.     {
  114.       ulog (LOG_ERROR, "fcntl (%s, FD_CLOEXEC): %s", zfile,
  115.         strerror (errno));
  116.       (void) close (o);
  117.       return NULL;
  118.     }
  119.  
  120.   if (fappend)
  121.     e = fdopen (o, (char *) "a");
  122.   else
  123.     e = fdopen (o, (char *) "w");
  124.  
  125.   if (e == NULL)
  126.     {
  127.       ulog (LOG_ERROR, "fdopen: %s", strerror (errno));
  128.       (void) close (o);
  129.     }
  130.  
  131.   return e;
  132. }
  133.